/*
 * @Author: your name
 * @Date: 2021-11-30 17:38:27
 * @LastEditTime: 2021-12-16 15:05:18
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \express-app\routes\login.js
 */
const express = require('express');
const router = express.Router();
const svgCaptcha = require('svg-captcha');
const UsersModel = require('../mongoose/users');
const { upload } = require('../utils/upload');
router.get('/', async (req, res, next) => {
  let data = await UsersModel.find({});
  //返回的是数组array
  console.log(Array.isArray(data));
  res.json({
    code: 1, //成功1 ， 失败2
    message: '成功',
    data,
  });
});

/**
 * 图形验证码
 */
router.get('/verify', function (req, res) {
  const captcha = svgCaptcha.create();
  // req.session.captcha = captcha.text;
  res.type('svg');
  res.status(200).send(captcha.data);
});

router.post('/register', async (req, res) => {
  res.send('register');
});
router.post('/upload', upload().single('img'), async (req, res) => {
  console.log(req.file);
  let { path } = req.file;
  let user = UsersModel({
    name: 'zhangsan',
    age: 36,
    img_url: path,
  });
  await user.save();
  res.send({
    msg: 'ok',
    img_url: path,
  });
});

module.exports = router;

// const token = jwt.sign({ foo: 'bar' }, 'shhhhh');
//   res.send('respond with a register');
// });

// jwt.verify(token, 'shhhhh', function(err, decoded) {
//   console.log(decoded.foo) // bar
// })
